<template src="./template.html"></template>

<script>
import ResizeMixin from 'mixins/resize'
import { PAGE_CONFIG } from 'common/consts';
import InfoMixin from '../infoMixins'
import { COL_INDEXS } from './consts';
import { getCourseCheckUser, batchReviewCourseUser, courseCheckReview } from '../models';

export default {
  mixins: [ResizeMixin, InfoMixin],
  data() {
    return {
      COL_INDEXS,
      isPageLoading: false,

      tableList: [],
      PAGE_CONFIG: PAGE_CONFIG,
      pageNo: PAGE_CONFIG.defaultPageNo,
      pageSize: PAGE_CONFIG.defaultPageSize,
      totalCount: 0,
      filterForm: {
        q: ''
      },

      selectStuData: [],
      isCheckAll: false
    };
  },
  methods: {
    // 自定义多选框全选
    $handleChangeCheckAll(val) {
      for (let i = 0; i < this.tableList.length; i += 1) {
        this.$set(this.tableList[i], 'checked', val)
        const index = this.selectStuData.findIndex(item => this.tableList[i].id === item.id)
        if (index === -1 && val) {
          this.selectStuData.push(this.tableList[i])
        } else if (index > -1 && !val) {
          this.selectStuData.splice(index, 1)
        }
      }
    },
    // 自定义多选框单选
    $handleChangeChecked(index) {
      this.isCheckAll = false
      const index1 = this.selectStuData.findIndex(item => this.tableList[index].id === item.id)
      if (index1 === -1 && this.tableList[index].checked) {
        this.selectStuData.push(this.tableList[index])
      } else if (index1 > -1) {
        this.selectStuData.splice(index1, 1)
      }

      if (this.tableList[index].checked) {
        const isCheckAllArr = this.tableList.filter(item => item.checked)
        if (isCheckAllArr.length === this.tableList.length) {
          this.isCheckAll = true
        }
      }
    },
    $search() {
      this.pageNo = 1
      this.$getTableList()
    },
    async $getTableList() {
      this.isPageLoading = true;
      const params = {
        ...this.filterForm,
        limit: this.pageSize,
        page: this.pageNo,
        courseId: this.courseId
      };
      try {
        const { data } = await getCourseCheckUser(params);
        this.tableList = data.list || []
        this.totalCount = data.total
        this.isPageLoading = false;
      } catch (err) {
        this.isPageLoading = false;
        this.$message.error(err.message || err.message || '获取机构列表出错')
      }
    },
    $handlePageChange() {
      this.$getTableList();
    },
    // 审核请求
    async $handleReviewHttp(params) {
      try {
        this.isPageLoading = true
        await batchReviewCourseUser(params)
        this.$message.success('审核成功')
        // if (this.tableData.length === 1) {
        //   this.pageNo = this.pageNo > 1 ? this.pageNo - 1 : this.pageNo
        // }
        this.$search()
        this.isCheckAll = false
      } catch (err) {
        this.isPageLoading = false
        this.$message.error(err.message || '审核失败')
      }
    },
    // 审核点击
    $handleReview() {
      const params = {
        ids: this.selectStuData.map(it => it.id),
        status: ''
      }
      this.$confirm('是否审核通过？', {
        type: 'warning',
        confirmButtonText: '不通过',
        cancelButtonText: '通过',
        confirmButtonClass: 'el-button--danger',
        cancelButtonClass: 'el-button--success'
      }).then(() => {
        params.status = 0
        this.$handleReviewHttp(params)
      }).catch(() => {
        params.status = 1
        this.$handleReviewHttp(params)
      });
    },
    async $handleSwitch(newVal) {
      const params = {
        courseId: this.courseId,
        isReview: newVal
      }
      this.isPageLoading = true
      try {
        await courseCheckReview(params)
        this.$message.success('操作成功')
        this.isPageLoading = false
        this.oldReviewState = newVal
        // this.$getCourseInfo()
      } catch (err) {
        this.infos.reviewState = this.oldReviewState
        this.isPageLoading = false
        this.$message.error(err.message || '操作失败')
      }
    }
  },
  created() {
    this.$getTableList();
  },
};
</script>
